define(["jquery", 'blasteroids/ship'], function($, Ship) {

    return function(){
        var self = this;
        this.ships = {};
        this.canvasSize = {
            'width':1200, 
            'height':600
        };
        this.started = false;
    
        this.addShip = function(ship){
            if(this.ships[ship.name] != null){
                return false;
            }else{
                this.ships[ship.name] = ship;
                return true;
            }
        };
        this.removeShip = function(name){
            delete this.ships[name];
        };
        this.setInfo = function(info){
            this.started = info.started;
            $.each(info.ships, function(index, value){
                if(self.ships[value.name] == null){
                    self.ships[value.name] = new Ship(value.name, value.angle, self.canvasSize, value.color, value.image);//(name, theta, canvasSize, color, image, useImage)
                }else{
                    self.ships[value.name].setInfo(value);
                }
            });
        };
        this.getInfo = function(){
            var shipInfos = new Array();
            $.each(this.ships, function(key, value){
                shipInfos.push(value.getInfo());
            });
            return {
                started:this.started,
                ships:shipInfos
            };
        };
        this.processAction = function(action){
            //console.log('Processing action: ' + JSON.stringify(action));
            this.ships[action.player].processAction(action.data);
        };
        this.draw = function(context, Images){
            $.each(this.ships, function(key, value){
                value.draw(context, Images);
            });
        };
        this.update = function(modifier){
            //update all the ships
            var shipList = this.ships;
            $.each(shipList, function(key, value){
                value.update(modifier);
            });
        
            //check collision on all the ships
            $.each(shipList, function(key, value){
                if(!value.dead){
                    $.each(shipList, function(innerKey, innerValue){
                        if(key!=innerKey){
                            $.each(innerValue.bullets, function(index, bullet){
                                if(value.contains(bullet.point)){
                                    value.destroy();
                                    innerValue.kills++;
                                }
                            });
                        }
                    });
                }
            });
        };
    };

});